﻿using MapleLeaf.Core.Extension;
using MapleLeaf.Model.CookBook.Menu.Entity;
using MapleLeaf.Model.CookBook.Organization.Entity;
using Mapster;

namespace MapleLeaf.Application.CookBook.Models {


    /// <summary>
    ///部门视图对象
    /// </summary>
    public class DepartmentVo {
        /// <summary>
        /// 子部门
        /// </summary>
        public List<DepartmentVo> Children { get; set; } = [];


        /// <summary>
        /// 部门ID 
        /// </summary>
        public string? Id { get; set; }

        /// <summary>
        /// 部门名称
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 部门编码
        /// </summary>
        public string Code { get; set; }

        /// <summary>
        /// 父部门ID
        /// </summary>
        public string? ParentId { get; set; }

        /// <summary>
        /// 菜单排序(数字越小排名越靠前)
        /// </summary>
        public int? Sort { get; set; }

        /// <summary>
        /// 状态
        /// </summary>
        public int Status { get; set; }
        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime? CreateTime { get; set; }
        /// <summary>
        /// 更新时间
        /// </summary>
        public DateTime? UpdateTime { get; set; }


    }
    /// <summary>
    /// 部门扩展类
    /// </summary>
    public static class DepartmentVoExtension {
        /// <summary>
        /// 获取部门树
        /// </summary>
        /// <param name="menus"></param>
        /// <returns></returns>
        public static List<DepartmentVo> GetTree(this List<DepartmentEntity> menus) {
            var list = menus.Where(m => m.ParentId == null || m.ParentId == 0).ToList();
            var voList = new List<DepartmentVo>();
            foreach (var item in list) {
                var im = item.Adapt<DepartmentVo>();
                GetChildren(im, menus);
                voList.Add(im);
            }
            return voList;
        }
        /// <summary>
        /// 获取子部门
        /// <param name="parent"></param>
        /// <param name="datalist"></param>
        private static void GetChildren(DepartmentVo parent, List<DepartmentEntity> datalist) {
            var children = datalist.Where(m => m.ParentId == (parent.Id?.ToLong() ?? 0)).ToList();
            foreach (var item in children) {
                var im = item.Adapt<DepartmentVo>();
                GetChildren(im, datalist);
                parent.Children.Add(im);
            }
        }

        /// <summary>
        /// 获取部门选项树
        /// </summary>
        /// <param name="datalist"></param>
        /// <returns></returns>
        public static List<OptionType> GetOptionTree(this List<DepartmentEntity> datalist) {
            var list = datalist.Where(m => m.ParentId == null || m.ParentId == 0).ToList();
            var voList = new List<OptionType>();
            foreach (var item in list) {
                var menu = new OptionType() {
                    Label = item.Name,
                    Value = item.Id
                };
                GetChildren(menu, datalist);
                voList.Add(menu);
            }
            return voList;
        }
        /// <summary>
        /// 获取子部门选项树
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="datalist"></param>
        private static void GetChildren(OptionType parent, List<DepartmentEntity> datalist) {
            var children = datalist.Where(m => m.ParentId == parent.Value.ToLong()).ToList();
            foreach (var item in children) {
                var menu = new OptionType() {
                    Label = item.Name,
                    Value = item.Id
                };
                GetChildren(menu, datalist);
                parent.Children?.Add(menu);
            }
        }
    }


  
}
